package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.ActorMenuDto;
import com.hualude.roadsign.dto.DTRequestDto;
import org.hibernate.query.NativeQuery;
import org.hibernate.transform.Transformers;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by xu-jiaHui on 2017/4/9.
 */
public class ActorMenuRepositoryImpl {
    @PersistenceContext
    private EntityManager em;

    public List<ActorMenuDto> getActorMenuByActorId(DTRequestDto dtRequestDto, Integer actorId) throws Exception {
        String sql = "select m.menuName,am.menuId,am.menuType,am.powerInsert,am.powerDelete,am.powerUpdate,am.powerSelect," +
                "am.powerCheck,am.amId from tb_actor_menu  am inner join tb_menu  m on am.menuId = m.menuId where " +
                "am.actorId=" + actorId + " and menuName like '%" + dtRequestDto.getSearch() + "%' order by am.menuId";
        Query query = em.createNativeQuery(sql);
        query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(ActorMenuDto.class));
        List<ActorMenuDto> actorMenuDtos = query.setFirstResult(dtRequestDto.getStart()).setMaxResults(dtRequestDto.getLength()).getResultList();
        return actorMenuDtos;
    }
}
